
from Common.logRoutine import *
import traceback
import threading
import urllib
import Queue

class DownloaderQueue( threading.Thread ):

  def __init__(self, requestQ, resultQ,  **kwds):
    threading.Thread.__init__(self, **kwds)
    
    self.log = getLogger()
    self.requestQ = requestQ
    self.resultQ = resultQ
    
  def run( self ):
    
    self.log.info("DownloaderQueue::run start")
    
    try:
    
      while True:
        url = self.requestQ.get_nowait()
        
        self.log.debug("getting %s"%(url))
        data = urllib.urlopen( url ).read()
        
        self.resultQ.put( data )
        
    except Queue.Empty, e:
      self.log.error("DownloaderQueue::run request queue empty")
      
    except:
      self.log.error("DownloaderQueue::run ex " + traceback.format_exc() )